<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Payment_m extends CI_Model {
   function get_ctu_chuyenkhoan(){
		$this->db->select("p.*, u.email, ntd.ten");
		$this->db->join('users u', 'p.user_id = u.id');
		$this->db->join('nhatuyendung ntd', 'ntd.user_id = u.id');
		$this->db->order_by('id', 'DESC');
		return
		$this->db->get('payment p')->result();
	}
	
	function get_payment($id){
		return 
		$this->db->where('id', $id)->get('payment')->row();
		echo $this->db->last_query();die;
	}
	
	function save($data){
		if($data['id']){
			$this->db->where('id', $data['id']);
			$this->db->update('payment', $data);
			return $data['id'];
		}else{
			$this->db->insert('payment', $data);
			return 
			$this->db->insert_id();
			// echo $this->db->last_query();die;
		}
	}
	
	function save_tien_ao($data){
		if($data['user_id']){
			$this->db->where('user_id', $data['user_id']);
			$row = $this->db->get('user_point')->row();
			if($row){
				$point = $row->so_diem;
				$point += $data['so_diem'];
				$data['so_diem'] = $point;
				$this->db->where('user_id', $data['user_id']);
				unset($data['user_id']);
				return $this->db->update('user_point', $data);
			}else{
				$this->db->insert('user_point', $data);
				return $this->db->insert_id();
			}
		}
	}
	
	function save_thanh_toan($data){
		$this->db->insert('thanh_toan', $data);
		return $this->db->insert_id();
	}
	
	function get_loi($code){
		$this->db->where('code', $code);
		return $this->db->get('card_errors')->row();
	}
}
